/************************************  

DESCRIPTION: Adds ARF information and Dr. characteristics
	to the member file

Datasets Used:
 (1) CountyChars2005_2015_noZIP
 (2) BENEQUARTER_FINAL_`i'
  
Datasets Created:
	(1) Bene_matchdata_qtr_2005_2012
	(2) Bene_matchdata


************************************/ 

set more off
capture log close
clear all

global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global datadoc "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global propscore "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2

*County characteristics
 tempfile cntychars
 use  "$propscore/CountyChars2005_2015_noZIP.dta" 
  duplicates drop
 sort ssa_sc year
 save `cntychars'
clear

*PATIENT CHARACTERISTICS  
use "$dataOut/BENEQUARTER_FINAL_2005.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2006.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2007.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2008.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2009.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2010.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2011.dta"
 append using "$dataOut/BENEQUARTER_FINAL_2012.dta"

 drop _merge
 capture drop tag
 gen year=year(quarter)
 drop if age<65

*Deal with encode on sex_pat
 drop if BENE_SEX == "0"
 drop if BENE_RACE == "0"
qui tab BENE_RACE, gen(RACE)
qui tab BENE_SEX, gen(SEX)
sort docid

*Note: Use cntyChars.dta

gen merger=vm_PROVIDER1 ~=.
foreach var of varlist vm_PROVIDER* {
replace merger=1 if `var' ~=.
}

gen ssa_sc=STATE_CODE +BENE_COUNTY_CD  

sort ssa_sc year
 merge m:1 ssa_sc year using `cntychars'
 drop if _merge==2
 drop _merge

qui tab urban_rural, gen(urbanrural)
qui tab urban_influence, gen(urbani)

save "$dataOut/Bene_matchdata_qtr_2005_2012.dta", replace

drop *age* BENE_DEATH_DT BENE_SEX_IDENT_CD BENE_RACE_CD TAX_NUM*    

*Create age at during each year - want similar cohorts
 forval i=2005/2012 {
 *ZIP
 gen ZIP`i'=BENE_ZIP if year==`i' 
 gsort BENE_ID -ZIP`i'
 by BENE_ID: replace ZIP`i'=ZIP`i'[1] 

 *STATE
 gen STATE`i'=STATE_CODE if year==`i' 
 gsort BENE_ID -STATE`i'
 by BENE_ID: replace STATE`i'=STATE`i'[1] 

 }

drop BENE_ZIP STATE_CODE  


 
*Keeping only the topline zipcode within a doctor-state
gen claims=1

drop PHYS_ZIP*

drop  race_pat sex_pat zip_pat DEAD BENE_COUNTY_CD PSTATE* fips* q_of_merger  qtr docid ssa_sc county mndate hh*

collapse (mean) urbanrural* urbani* pop_density  (min) firmct BENE_BIRTH_DT (max) physct vm_* hosp* SEX* RACE* PSPEC* new* mx* merger (sum) claims, by(BENE_ID ZIP* STATE* )  

save "$dataOut/Bene_matchdata.dta" , replace
clear


*PHYSICIAN CHARACTERISTICS  

*Created in earlier file
use "$datadoc/DR_CHARS_PRE_2005_2012.dta" 
sort docid year
save, replace
clear

use NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID using "$dataOut/ANALYSIS_V3_2005.dta"  
 append using "$dataOut/ANALYSIS_V3_2006.dta", keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)  
 append using "$dataOut/ANALYSIS_V3_2007.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)
 append using "$dataOut/ANALYSIS_V3_2008.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)
 append using "$dataOut/ANALYSIS_V3_2009.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)
 append using "$dataOut/ANALYSIS_V3_2010.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)
 append using "$dataOut/ANALYSIS_V3_2011.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)
 append using "$dataOut/ANALYSIS_V3_2012.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID BENE_ID)
  drop if BENE_ID ==""

 gen docid=NPI
 replace docid=UPIN if NPI=="" & docid==""

 drop UPIN NPI npi upin
 
gen year=year(quarter)
drop if year==.
drop quarter

duplicates drop
duplicates tag BENE_ID year docid, gen(tag)
*This affects .03% of the observations
drop if PHYS_STATE =="" & tag>0
drop if TAX =="" & tag>0
drop tag
sort BENE_ID year docid PHYS_SP PHYS_Z
by BENE_ID year docid: keep if _n==1

bysort BENE_ID year: gen docCnt=_N

keep docid TAX_NUM1 BENE_ID year docCnt
drop if year<2005

sort docid year
merge m:1 docid year using "$datadoc/DR_CHARS_PRE_2005_2012.dta"
drop if _merge==2
drop _merge

levelsof PHYS_SPCLTY1, local(psploc)

foreach i of local psploc {
 foreach var of varlist PHYS_SPCLTY1 {
di "`i'"
di "`var'"
gen psps`i'=`var' == "`i'"
 }
}
drop PHYS_ZIP* PHYS_STATE* PHYS_SPCLTY1

foreach i of local psploc {
 foreach var of varlist PHYS_SPCLTY* {
di "`i'"
di "`var'"
replace psps`i'=1 if `var' == "`i'"
 }
}


collapse (mean) firmsize (max) fs* psps* doc_Cnt*, by(BENE_ID year )  
 
save "$dataOut/Bene_doc_match.dta" , replace



